package com.zhika.zishi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.EditStrategyGridAdapter;
import com.zhika.zishi.beans.EditStrategyGridBean;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.customview.customlayout.FreeNumLayout;
import com.zhika.zishi.interfaces.TitleInterface;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/17.
 */
public class EditStrategyActivity extends BaseActivity implements TitleInterface {

    private GridView gridView;

    private ArrayList<EditStrategyGridBean> editStrategyList = new ArrayList<>();

    private EditStrategyGridAdapter editAdapter;

    private TitleView titleView;

    private TextView modifyText;

    //自定义的布局，可以实现自由分配子控件个数的布局
    private FreeNumLayout freeNumLayout;

    //测试数据
    private String[] strs = new String[]{
            "频道","频道频道",
            "频道频道频道","频道频道频道频道",
            "频道","频道频道"
    };

    //显示跟多状态的text
    private TextView moreTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_strategy_layout);

        addTestData();

        initView();
    }

    private void initView(){
        gridView = (GridView) findViewById(R.id.edit_strategy_grid_view);
        editAdapter = new EditStrategyGridAdapter(EditStrategyActivity.this,editStrategyList);
        gridView.setAdapter(editAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(EditStrategyActivity.this,EditingStrategyActivity.class);
                startActivity(intent);
            }
        });

        titleView = (TitleView) findViewById(R.id.edit_strategy_title_view);
        titleView.setTitleViewInterface(this);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("编辑");
        titleView.setRightButtonText("发布");

        modifyText = (TextView) findViewById(R.id.edit_strategy_modify_channel_text);
        modifyText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(EditStrategyActivity.this,EditChannelActivity.class);
                startActivity(intent);
            }
        });

        moreTextView = (TextView) findViewById(R.id.edit_strategy_more_channel_text);
        freeNumLayout = (FreeNumLayout) findViewById(R.id.edit_strategy_free_num_layout);

        for ( int i = 0;i<strs.length;i++){
            final TextView textView = (TextView) LayoutInflater.from(EditStrategyActivity.this).inflate(R.layout.my_text_view, freeNumLayout, false);
            textView.setText(strs[i]);
            freeNumLayout.addView(textView);
        }

        freeNumLayout.post(new Runnable() {
            @Override
            public void run() {
                int num = freeNumLayout.getShowNum();
                if (num == strs.length){
                    moreTextView.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 测试数据
     */
    private void addTestData(){
        for (int i = 0;i<10;i++){
            EditStrategyGridBean bean = new EditStrategyGridBean();
            bean.setContentStr("攻略内容攻略内容攻略内容攻略内容攻略内容攻略内容");
            int page = i + 1;
            bean.setPageNumStr(""+page);

            editStrategyList.add(bean);
        }
    }



    @Override
    public void leftTitleListener() {
        finish();
    }

    @Override
    public void middleTitleListener() {

    }

    @Override
    public void rightTitleListener() {
        Toast.makeText(EditStrategyActivity.this,"发布",Toast.LENGTH_SHORT).show();
    }
}
